package com.houdoudou.customer.sqlite;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.houdoudou.bean.Branch;
import com.houdoudou.bean.Car;
import com.houdoudou.bean.Machine;
import com.houdoudou.bean.Product;
import com.houdoudou.bean.Supplier;
import com.houdoudou.customer.buy.ShopAct;
import com.houdoudou.customer.common.Constant;

public class CarDao {

	
	private DBHelper dbhelper;
	private SQLiteDatabase db;
	Context context;
	String table="car";
	public CarDao(Context context) {
		dbhelper= new DBHelper(context);
		db=dbhelper.getWritableDatabase();
		this.context=context;
	}
	
	public boolean insert(JSONObject carJO){
		boolean result=true;
		           System.out.println("insert---------"+carJO);
		         try{
			           ContentValues cv = new ContentValues();	
			   
			           cv.put(Car.MACHINE, carJO.getInt(Car.MACHINE));          
			           cv.put(Car.TIMEFLAG, carJO.getInt(Car.TIMEFLAG));
			           cv.put(Car.SUPPLIER, carJO.getString(Car.SUPPLIER));				           
			           cv.put(Car.PRODUCT ,carJO.getInt(Car.PRODUCT));	
			           cv.put(Car.BRANCH, carJO.getInt(Car.BRANCH));	
			           cv.put(Car.NUMBER, carJO.getInt(Car.NUMBER));			           
			           long rowid = db.insert(table, null, cv);			
			          Toast.makeText(context, "已插入一条测试数据，carJO =" + carJO+"  ROWS affected"+ rowid, Toast.LENGTH_SHORT).show();
			
		         }catch(Exception e){
		        	 e.printStackTrace();
		        	 result=false;
		         }
		
		return result;
	} 
	

	public boolean updateOneRecord(JSONObject carJO){
		boolean result=true;
		 System.out.println("update---------"+carJO);
		         try{
			           
			          //修改条件
			           
						   String whereClause= Car.MACHINE+"=? and "+Car.TIMEFLAG+"=? and "+Car.SUPPLIER+"=? and "+Car.PRODUCT+"=? and "+Car.BRANCH+"=? ";
						   String [] whereArgs= new String[5];
						   whereArgs[0]= carJO.getInt(Car.MACHINE)+"";
						   whereArgs[1]= carJO.getInt(Car.TIMEFLAG)+"";
						   whereArgs[2]= carJO.getString(Car.SUPPLIER);
						   whereArgs[3]=carJO.getInt(Car.PRODUCT)+"";
						   whereArgs[4]=carJO.getInt(Car.BRANCH)+"";
						   
						    ContentValues cv = new ContentValues();	
				            cv.put(Car.NUMBER, carJO.getInt(Car.NUMBER));
				            
				    	int rowsAffected =db.update(table,cv,whereClause,whereArgs);//执行修改		    	
			          	
				    	 Toast.makeText(context, "更新一条数据，carJO =" + carJO+"  ROWS affected"+ rowsAffected, Toast.LENGTH_SHORT).show();
			
		         }catch(Exception e){
		        	 e.printStackTrace();
		        	 result=false;
		         }
		
		return result;
	} 
	public JSONObject queryOneBranch(JSONObject carJO){
		JSONObject record= new JSONObject();
		 System.out.println("queryOneBranch---------"+carJO);
		   try{
			   
			   String baseClause="SELECT * FROM "+table+" where ";
			   String whereClause= Car.MACHINE+"=? and "+Car.TIMEFLAG+"=? and "+Car.SUPPLIER+"=? and "+Car.PRODUCT+"=? and "+Car.BRANCH+"=? ";
			   String [] whereArgs= new String[5];
			   whereArgs[0]= carJO.getInt(Car.MACHINE)+"";
			   whereArgs[1]= carJO.getInt(Car.TIMEFLAG)+"";
			   whereArgs[2]= carJO.getString(Car.SUPPLIER);
			   whereArgs[3]=carJO.getInt(Car.PRODUCT)+"";
			   whereArgs[4]=carJO.getInt(Car.BRANCH)+"";
			  // db.execSQL("delete from Tb_Selected_Branch where Tb_Selected_Branch=?",params);
			   System.out.println("queryOneBranch  sql=      "+baseClause+whereClause);
			   Cursor c=  db.rawQuery(baseClause+whereClause, whereArgs);
			   if(c.moveToNext()){
				     
				     record.put("success",true);
				     record.put(Car.MACHINE,  c.getInt(c.getColumnIndex(Car.MACHINE)));
		        	 record.put(Car.SUPPLIER,  c.getString(c.getColumnIndex(Car.SUPPLIER)));
		        	 record.put(Car.TIMEFLAG,  c.getInt(c.getColumnIndex(Car.TIMEFLAG)));
		        	 record.put(Car.PRODUCT,  c.getInt(c.getColumnIndex(Car.PRODUCT)));
		        	 record.put(Car.BRANCH,  c.getInt(c.getColumnIndex(Car.BRANCH)));
		        	 record.put(Car.NUMBER,  c.getInt(c.getColumnIndex(Car.NUMBER)));
			   }else{
				   record.put("success",false);
			   }
		   }catch( Exception e){
			  e.printStackTrace();
			  record.put("success",false);
			 
		   }finally{ 
			  Toast.makeText(context, "查询单条记录" +record.toString(), Toast.LENGTH_SHORT).show();
		   }
		  
		   return record;
	}
	
	public JSONArray  queryByProduct(JSONObject carJO){
		  //Toast.makeText(context, "查询  Product_ID =" + Product_ID, Toast.LENGTH_SHORT).show();
		 System.out.println("queryByProduct---------"+carJO);
		JSONArray result= new JSONArray();
		 try{
			 String baseClause="SELECT * FROM "+table+" where ";
			   String whereClause= Car.MACHINE+"=? and "+Car.TIMEFLAG+"=? and "+Car.SUPPLIER+"=? and "+Car.PRODUCT+"=? ";
			   String [] whereArgs= new String[4];
			   whereArgs[0]= carJO.getInt(Car.MACHINE)+"";
			   whereArgs[1]= carJO.getInt(Car.TIMEFLAG)+"";
			   whereArgs[2]= carJO.getString(Car.SUPPLIER);
			   whereArgs[3]=carJO.getInt(Car.PRODUCT)+"";
			  // db.execSQL("delete from Tb_Selected_Branch where Tb_Selected_Branch=?",params);
			   Cursor c=  db.rawQuery(baseClause+whereClause, whereArgs);
		
	        while (c.moveToNext()) {  
	        	 JSONObject record= new JSONObject();
	        	 record.put(Car.MACHINE,  c.getInt(c.getColumnIndex(Car.MACHINE)));
	        	 record.put(Car.SUPPLIER,  c.getString(c.getColumnIndex(Car.SUPPLIER)));
	        	 record.put(Car.TIMEFLAG,  c.getInt(c.getColumnIndex(Car.TIMEFLAG)));
	        	 record.put(Car.PRODUCT,  c.getInt(c.getColumnIndex(Car.PRODUCT)));
	        	 record.put(Car.BRANCH,  c.getInt(c.getColumnIndex(Car.BRANCH)));
	        	 record.put(Car.NUMBER,  c.getInt(c.getColumnIndex(Car.NUMBER)));
	        	// record.put("Is_Selected",false);
	        	   result.add(record);        	
	        	   
	        }
	       
	        c.close();  
		      }catch(Exception e){
		    	  e.printStackTrace();
		      }
		     Toast.makeText(context, "查询产品" +result.toString(), Toast.LENGTH_SHORT).show();
		return result;		
	} 
	
	public JSONArray  queryBySupplier(JSONObject carJO ){
		  //Toast.makeText(context, "查询  Supplier_Telnum =" + Supplier_Telnum, Toast.LENGTH_SHORT).show();
		JSONArray result= new JSONArray();
		      try{
		    	  String baseClause="SELECT * FROM "+table+" where ";
				   String whereClause= Car.MACHINE+"=? and "+Car.TIMEFLAG+"=? and "+Car.SUPPLIER+"=? ";
				   String [] whereArgs= new String[3];
				   whereArgs[0]= carJO.getInt(Car.MACHINE)+"";
				   whereArgs[1]= carJO.getInt(Car.TIMEFLAG)+"";
				   whereArgs[2]= carJO.getString(Car.SUPPLIER);
				  // db.execSQL("delete from Tb_Selected_Branch where Tb_Selected_Branch=?",params);
				   Cursor c=  db.rawQuery(baseClause+whereClause, whereArgs);
	        while (c.moveToNext()) {  
	        	 JSONObject record= new JSONObject();
	        	 record.put(Car.MACHINE,  c.getInt(c.getColumnIndex(Car.MACHINE)));
	        	 record.put(Car.SUPPLIER,  c.getString(c.getColumnIndex(Car.SUPPLIER)));
	        	 record.put(Car.TIMEFLAG,  c.getInt(c.getColumnIndex(Car.TIMEFLAG)));
	        	 record.put(Car.PRODUCT,  c.getInt(c.getColumnIndex(Car.PRODUCT)));
	        	 record.put(Car.BRANCH,  c.getInt(c.getColumnIndex(Car.BRANCH)));
	        	 record.put(Car.NUMBER,  c.getInt(c.getColumnIndex(Car.NUMBER)));  	
	        	 result.add(record);        	
	        	   
	        }
	        c.close();  
		      }catch(Exception e){
		    	  e.printStackTrace();
		      }
		      //Toast.makeText(context, "查询产品" +result.toString(), Toast.LENGTH_SHORT).show();
		return result;		
	} 

	public JSONArray  queryAll( ){
		  //Toast.makeText(context, "查询  所有", Toast.LENGTH_SHORT).show();
		JSONArray result= new JSONArray();
		      try{
		 Cursor c = db.rawQuery("SELECT * FROM Tb_Selected_Branch", null); 
	        while (c.moveToNext()) {  
	        	 JSONObject record= new JSONObject();
	        	 record.put(Supplier.TELNUM,  c.getString(c.getColumnIndex(Supplier.TELNUM)));
	        	 record.put(Product.ID,  c.getInt(c.getColumnIndex(Product.ID)));
	        	 record.put(Product.NAME,  c.getString(c.getColumnIndex(Product.NAME)));
	        	 record.put(Branch.ID,  c.getInt(c.getColumnIndex(Branch.ID)));
	        	 record.put(Branch.NAME,  c.getString(c.getColumnIndex(Branch.NAME)));
	        	 record.put("Branch_Num",  c.getInt(c.getColumnIndex("Branch_Num")));
	        	 record.put(Branch.PRICE,  c.getDouble(c.getColumnIndex(Branch.PRICE)));	
	        	 record.put(Branch.VOLUME, c.getInt(c.getColumnIndex(Branch.VOLUME)));
	        	 record.put("Arrive_Time", c.getString(c.getColumnIndex("Arrive_Time")));
	        	
	        	   result.add(record);        	
	        }
	        c.close();  
		      }catch(Exception e){
		    	  e.printStackTrace();
		      }
		      //Toast.makeText(context, "查询产品" +result.toString(), Toast.LENGTH_SHORT).show();
		return result;		
	} 
	public boolean deleteOneRecord(JSONObject carJO){
		 boolean result=false; int rowAffected=0;
		 System.out.println("deleteOneRecord---------"+carJO);
		   try{
			   
			   String whereClause= Car.MACHINE+"=? and "+Car.TIMEFLAG+"=? and "+Car.SUPPLIER+"=? and "+Car.PRODUCT+"=? and "+Car.BRANCH+"=? ";
			   String [] whereArgs= new String[5];
			   whereArgs[0]= carJO.getInt(Car.MACHINE)+"";
			   whereArgs[1]= carJO.getInt(Car.TIMEFLAG)+"";
			   whereArgs[2]= carJO.getString(Car.SUPPLIER);
			   whereArgs[3]=carJO.getInt(Car.PRODUCT)+"";
			   whereArgs[4]=carJO.getInt(Car.BRANCH)+"";
			  // db.execSQL("delete from Tb_Selected_Branch where Tb_Selected_Branch=?",params);
			   rowAffected=  db.delete(table, whereClause, whereArgs);
			   result=true;
		   }catch( Exception e){
			  e.printStackTrace();
			  result=false;
		   }finally{ 
			   Toast.makeText(context, "删除产品" +carJO.toString()+"  rowAffected="+rowAffected, Toast.LENGTH_SHORT).show();
		   }
		  
		   return result;
	}
	
	public boolean deleteOneProduct(JSONObject carJO){
		 System.out.println("deleteOneProduct---------"+carJO);
		boolean result=false;int rowAffected = 0;
		   try{
			   
			   String whereClause= Car.MACHINE+"=? and "+Car.TIMEFLAG+"=? and "+Car.SUPPLIER+"=? and "+Car.PRODUCT+"=? ";
			   String [] whereArgs= new String[4];
			   whereArgs[0]= carJO.getInt(Car.MACHINE)+"";
			   whereArgs[1]= carJO.getInt(Car.TIMEFLAG)+"";
			   whereArgs[2]= carJO.getString(Car.SUPPLIER);
			   whereArgs[3]=carJO.getInt(Car.PRODUCT)+"";		 
			   rowAffected= db.delete(table, whereClause, whereArgs);
			   result=true;
		   }catch( Exception e){
			  e.printStackTrace();
			  result=false;
		   }finally{ 
			   Toast.makeText(context, "删除产品" +carJO.toString()+"  rowAffected="+rowAffected, Toast.LENGTH_SHORT).show();
		   }
		  
		   return result;
	}
	public boolean deleteOneSupplier(JSONObject carJO){
		boolean result=false; int rowAffected=-1;
		   try{
			   String whereClause= Car.MACHINE+"=? and "+Car.TIMEFLAG+"=? and "+Car.SUPPLIER+"=? ";
			   String [] whereArgs= new String[3];
			   whereArgs[0]= carJO.getInt(Car.MACHINE)+"";
			   whereArgs[1]= carJO.getInt(Car.TIMEFLAG)+"";
			   whereArgs[2]= carJO.getString(Car.SUPPLIER);
			   rowAffected= db.delete(table, whereClause, whereArgs);
			   result=true;
		   }catch( Exception e){
			  e.printStackTrace();
			  result=false;
		   }finally{ 
			   Toast.makeText(context, "删除商家产品" +carJO.toString()+"  rowAffected="+rowAffected, Toast.LENGTH_SHORT).show();
		   }
		  
		   return result;
	}
	public boolean deleteAll(){
		boolean result=false;
		   try{
			  // db.execSQL("delete from Tb_Selected_Branch where Tb_Selected_Branch=?",params);
			   db.delete("Tb_Selected_Branch", null, null);
			   result=true;
		   }catch( Exception e){
			  e.printStackTrace();
			  result=false;
		   }finally{ 
			   //Toast.makeText(context, "删除一个店所有购物车内东西" +branch.toString(), Toast.LENGTH_SHORT).show();
		   }
		  
		   return result;
	}
	
	
}
